package com.xquant.trade.account.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xquant.common.security.annotation.RequiresPermissions;
import com.xquant.common.security.utils.SecurityUtils;
import com.xquant.trade.account.entity.Counterparty;
import com.xquant.trade.account.mapper.CounterpartyMapper;
import com.xquant.trade.account.service.AccountService;
import com.xquant.trade.account.service.CounterpartyService;
import com.xquant.trade.utils.ResponseVo;
import com.xquant.trade.trade.entity.PartyStatus;
import com.xquant.trade.utils.CommResponse;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;

import static com.xquant.common.core.utils.PageUtils.startPage;

/**
 *
 * newCloud 对手账户管理
 * */
@RestController
@RequestMapping("counterparty")
public class CounterpartyController {

    @Autowired
    private CounterpartyService counterpartyService;
    @Autowired
    private CounterpartyMapper counterpartyMapper;

    @Autowired
    private AccountService accountService;


    static String sm = "操作成功";
    String fold = "counterparty";

    @RequestMapping(value = "/queryCounterpartyByPage.action")
    @RequiresPermissions("account:cpaccount:list")
    public PageInfo<Counterparty> queryCounterpartyByPage(@RequestParam Map<String, Object> param) {
        startPage();
        param.put("userId", SecurityUtils.getUserId());
        String status = MapUtils.getString(param, "status");
        if (StringUtils.isNotBlank(status)) {
            param.put("status", status.split(","));
        }
        PageInfo<Counterparty> pageInfo = counterpartyService.queryCounterpartyByPage(param);
        return pageInfo;
    }

    @RequestMapping(value = "/queryCounterparty4Combox.action")
    public Object queryCounterparty4Combox(@RequestParam Map<String, Object> param) {
        param.put("userId", SecurityUtils.getUserId());
        String[] status = {"1", "2"};
        param.put("status", status);
        PageHelper.startPage(1, 60).setReasonable(true);
        PageInfo<Counterparty> pageInfo = counterpartyService.queryCounterpartyCombox(param);
        return pageInfo;
    }

    /**
     * 查询 下拉列表
     */
    @RequestMapping(value = "/queryCounterparty.action")
    public Object queryCounterparty(HttpServletRequest request) {
        Map<String, Object> params = new WeakHashMap<>();
        params.put("condition", request.getParameter("query"));
        if (StringUtils.isBlank(request.getParameter("query"))) {
            params.put("condition", request.getParameter("condition"));
        }
        List<Counterparty> list = counterpartyService.queryCounterparty(params);
        return ResponseVo.success(list);
    }

    @RequestMapping(value = "/queryByPartyId.action")
    public Object queryByPartyId(@RequestParam Map<String, Object> param) {
        List<Counterparty> list = counterpartyService.queryByPartyId(param);
        return ResponseVo.success(list);
    }

    @RequestMapping(value = "/insertCounterparty.action")
    @RequiresPermissions("account:cpaccount:add")
    public CommResponse insertCounterparty(@RequestBody Counterparty cp) {
        boolean flag = counterpartyService.checkPartyName(cp);
        if (flag){
            counterpartyService.insertCounterparty(cp);
            return CommResponse.success();
        }else {
            return CommResponse.failure("对手名称重复");
        }

    }

    @RequestMapping(value = "/updateCounterparty.action")
    @RequiresPermissions("account:cpaccount:edit")
    public CommResponse updateCounterparty(@RequestBody Counterparty cp) {
        boolean flag = counterpartyService.checkPartyName(cp);
        if (flag){
            counterpartyService.updateCounterparty(cp);
            return CommResponse.success("修改成功");
        }else {
            return CommResponse.failure("对手名称重复");
        }
    }

    @RequestMapping(value = "/activeCounterparty.action")
    @RequiresPermissions("account:cpaccount:start")
    public CommResponse activeCounterparty(@RequestBody Counterparty cp) {
        cp.setStatus(PartyStatus.ACTIVE);
        counterpartyService.updateCounterpartyStatus(cp);
        return CommResponse.success();
    }

    @RequestMapping(value = "/disableCounterparty.action")
    @RequiresPermissions("account:cpaccount:stop")
    public CommResponse disableCounterparty(@RequestBody Counterparty cp) {
        cp.setStatus(PartyStatus.DISABLE);
        counterpartyService.updateCounterpartyStatus(cp);
        return CommResponse.success();
    }

    @RequestMapping(value = "/deleteCounterparty.action")
    @RequiresPermissions("account:cpaccount:remove")
    public CommResponse deleteCounterparty(@RequestBody Counterparty cp) {
        Map map = new HashMap();
        map.put("partyId", cp.getPartyId());
        map.put("accId", cp.getPartyId());
        List list = counterpartyService.queryCounterpartyTrade(cp);
        if (list.size() > 0) {
            return new CommResponse(false, "此客户已存在交易，无法删除！");
        }
        counterpartyService.deleteCounterparty(cp);
        return new CommResponse(true, sm);
    }


}
